package hxy.bytecode.algorithm;

import java.util.ArrayList;

/**
 * @author eric
 * @program bytecode
 * @description 栈优先
 * @date 2020/4/11
 */
public class Main {

    public static void main(String[] args) {

//        Scanner scanner = new Scanner(System.in);
//        String s = scanner.nextLine();
        String s = "A1,B2,C2,C3,a91,d03";
        ArrayList<Element> analysis = analysis(s);
        ArrayList<Element> sort = sort(analysis);
        print(sort);


    }


    /**
     * @param input
     * @return
     */
    public static ArrayList<Element> analysis(String input) {
        ArrayList<Element> arrayList = new ArrayList<>();
        String[] inputs = input.split(",");
        if (input.length() > 100) {
            return null;
        }
        for (String in : inputs) {
            char[] chars = in.toCharArray();

            if (chars.length < 2) {
                continue;
            }
            Element element = new Element();
            String alp = String.valueOf(chars[0]);
            element.setAlp(alp);


            String gradeStr = in.substring(1);
            Integer integer = Integer.valueOf(gradeStr);

            element.setGrade(integer);
            arrayList.add(element);
        }
        return arrayList;
    }

    /**
     * 排序，冒泡(快速)排序,从小到大输出
     */
    public static ArrayList<Element> sort(ArrayList<Element> list) {

        for (int i = list.size() - 1; i > 0; i--) {

            for (int j = 0; j < i; j++) {
                // 从大到小排序
                Element elementj = list.get(j);
                Element elementj1 = list.get(j + 1);
                if (elementj.getGrade() < elementj1.getGrade()) {
//                    Element tmp = elementj;
//                    elementj = elementj1;
//                    elementj1 = tmp;
                    list.set(j, elementj1);
                    list.set(j + 1, elementj);
//                    list.set(j)
                }
            }

        }
        return list;
    }

    /**
     * 输出结果
     *
     * @param list
     */
    public static void print(ArrayList<Element> list) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            Element element = list.get(i);
            String alp = element.getAlp();
            Integer grade = element.getGrade();
            String value = alp + grade;
            stringBuffer.append(value);
            if (i != list.size() - 1) {
                stringBuffer.append(",");
            }
        }
        System.out.println(stringBuffer);
    }


}

class Element {
    /**
     * 字母
     */
    private String alp;
    /**
     * 级别
     */
    private Integer grade;

    public String getAlp() {
        return alp;
    }

    public void setAlp(String alp) {
        this.alp = alp;
    }

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }
}
